package com.ruoyi.hospital.service.storeInfo.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.domain.shop.PharmacyDrugSku;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDrug;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDrugNon;
import com.ruoyi.hospital.domain.storeInfo.PharmacyHerb;
import com.ruoyi.hospital.domain.storeInfo.vo.PharmacyDrugNonVo;
import com.ruoyi.hospital.domain.storeInfo.vo.PharmacyDrugVo;
import com.ruoyi.hospital.domain.storeInfo.vo.PharmacyHerbVo;
import com.ruoyi.hospital.mapper.shop.PharmacyDrugSkuMapper;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyDrugMapper;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyDrugNonMapper;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyHerbMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyDruginfoMapper;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDruginfo;
import com.ruoyi.hospital.service.storeInfo.IPharmacyDruginfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 药品公共信息Service业务层处理
 * 
 * @author wendao
 * @date 2024-09-12
 */
@Service
public class PharmacyDruginfoServiceImpl implements IPharmacyDruginfoService 
{
    @Autowired
    private PharmacyDruginfoMapper pharmacyDruginfoMapper;


    @Autowired
    private PharmacyDrugSkuMapper pharmacyDrugSkuMapper;

    @Autowired
    private PharmacyDrugMapper pharmacyDrugMapper;
    @Autowired
    private PharmacyDrugNonMapper pharmacyDrugNonMapper;
    @Autowired
    private PharmacyHerbMapper pharmacyHerbMapper;

    /**
     * 查询药品公共信息
     * 
     * @param id 药品公共信息主键
     * @return 药品公共信息
     */
    @Override
    public PharmacyDruginfo selectPharmacyDruginfoById(Integer id)
    {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoById(id);
    }

    @Override
    public PharmacyDrugVo selectPharmacyDruginfoAnddrug(Integer id) {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoAnddrug(id);
    }

    @Override
    public PharmacyDrugVo selectPharmacyDruginfoAndnodrug(Integer id) {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoAndnodrug(id);
    }

    @Override
    public PharmacyDrugVo selectPharmacyDruginfoAndherb(Integer id) {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoAndherb(id);
    }

    /**
     * 查询药品公共信息列表
     * 
     * @param pharmacyDruginfo 药品公共信息
     * @return 药品公共信息
     */
    @Override
    public List<PharmacyDruginfo> selectPharmacyDruginfoList(PharmacyDruginfo pharmacyDruginfo)
    {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoList(pharmacyDruginfo);
    }

    @Override
    public List<PharmacyDruginfo> selectPharmacyDruginfoListAPP(PharmacyDruginfo pharmacyDruginfo) {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoListAPP(pharmacyDruginfo);
    }

    @Override
    public List<PharmacyDruginfo> selectPharmacyDruginfoListLike(String keyWord) {
        return pharmacyDruginfoMapper.selectPharmacyDruginfoListLike(keyWord);
    }

    /**
     * 新增药品公共信息
     * 
     * @param pharmacyDruginfo 药品公共信息
     * @return 结果
     */
    @Override
    public int insertPharmacyDruginfo(PharmacyDruginfo pharmacyDruginfo)
    {
        pharmacyDruginfo.setCreateTime(DateUtils.getNowTimeSecond());
        return pharmacyDruginfoMapper.insertPharmacyDruginfo(pharmacyDruginfo);
    }

    /**
     * 修改药品公共信息
     * 
     * @param pharmacyDruginfo 药品公共信息
     * @return 结果
     */
    @Override
    public int updatePharmacyDruginfo(PharmacyDruginfo pharmacyDruginfo)
    {
        pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return pharmacyDruginfoMapper.updatePharmacyDruginfo(pharmacyDruginfo);
    }

    /**
     * 批量删除药品公共信息
     * 
     * @param ids 需要删除的药品公共信息主键
     * @return 结果
     */
    @Override
    public int deletePharmacyDruginfoByIds(Integer[] ids)
    {
        return pharmacyDruginfoMapper.deletePharmacyDruginfoByIds(ids);
    }

    /**
     * 删除药品公共信息信息
     * 
     * @param id 药品公共信息主键
     * @return 结果
     */
    @Override
    public int deletePharmacyDruginfoById(Integer id)
    {
        return pharmacyDruginfoMapper.deletePharmacyDruginfoById(id);
    }

    /*
    * 发布药品
    * */
    @Override
    @Transactional
    public int releasedPharmacyDrug(PharmacyDrugVo pharmacyDrugVo) {
        int m = 1;
        try {
            if(StringUtils.isNullOrZero(pharmacyDrugVo.getId())){  //表示添加商品
                //添加主表
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyDrugVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.insertPharmacyDruginfo(pharmacyDruginfo);


                //添加附加表
                PharmacyDrug pharmacyDrug = new PharmacyDrug();
                BeanUtils.copyProperties(pharmacyDrugVo, pharmacyDrug);
                pharmacyDrug.setCreateTime(DateUtils.getNowTimeSecond());
                pharmacyDrug.setPhaId(pharmacyDruginfo.getId());
                pharmacyDrugMapper.insertPharmacyDrug(pharmacyDrug);

                //添加sku表
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyDruginfo.getId());
                List<PharmacyDrugSku> skuList = pharmacyDrugVo.getSkuList();
                // 添加sku
                if(skuList!=null){
                    skuList.forEach(sku ->{
                        sku.setProductId(pharmacyDruginfo.getId());
                        pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                    });
                }
            }else{
                //首先修改主表信息
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyDrugVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.updatePharmacyDruginfo(pharmacyDruginfo);

                //处理附加表信息
                PharmacyDrug pharmacyDrug = new PharmacyDrug();
                BeanUtils.copyProperties(pharmacyDrugVo, pharmacyDrug);
                if(pharmacyDrugVo.getDrugId()!=null){ //表示要修改附表的
                    pharmacyDrug.setId(pharmacyDrugVo.getDrugId());
                    pharmacyDrugMapper.updatePharmacyDrug(pharmacyDrug);
                }else{
                    pharmacyDrug.setCreateTime(DateUtils.getNowTimeSecond());
                    pharmacyDrug.setPhaId(pharmacyDruginfo.getId());
                    pharmacyDrugMapper.insertPharmacyDrug(pharmacyDrug);
                }

                //处理sku
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyDrugVo.getId());
                Map<Integer, PharmacyDrugSku> skuMap = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku).stream().collect(Collectors.toMap(it -> it.getId(), it -> it));
                if(skuMap==null){ //直接添加
                    List<PharmacyDrugSku> skuList = pharmacyDrugVo.getSkuList();
                    // 添加sku
                    if(skuList!=null){
                        skuList.forEach(sku ->{
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }
                }else{ //修改
                    //针对已有的进行修改
                    List<PharmacyDrugSku> updateList = pharmacyDrugVo.getSkuList().stream().filter(it -> it.getId() != null).collect(Collectors.toList());
                    if (updateList!=null) {
                        updateList.forEach(it->{
                            PharmacyDrugSku sku = skuMap.get(it.getId());
                            sku.setUpdateTime(DateUtils.getNowTimeSecond());
                            sku.setCreateTime(DateUtils.getNowTimeSecond());
                            sku.setPrice(it.getPrice());
                            sku.setSpData(it.getSpData());
                            sku.setPic(it.getPic());
                            sku.setStock(it.getStock());
                            pharmacyDrugSkuMapper.updatePharmacyDrugSku(sku);
                        });
                    }

                    //针对没有的进行新增
                    List<PharmacyDrugSku> addList = pharmacyDrugVo.getSkuList().stream().filter(it -> it.getId() == null).collect(Collectors.toList());
                    if (addList!=null) {
                        addList.forEach(sku -> {
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }

                    //删除
                    List<Integer> idList = pharmacyDrugVo.getSkuList().stream().filter(it -> it.getId() != null).map(it -> it.getId()).collect(Collectors.toList());
                    List<Integer> deleteIds = skuMap.keySet().stream().filter(it -> !idList.contains(it)).collect(Collectors.toList());
                    for(int i=0;i<deleteIds.size();i++){
                        pharmacyDrugSkuMapper.deletePharmacyDrugSkuById(deleteIds.get(i));
                    }

                }
            }
        } catch (BeansException e) {
            m=0;
            e.printStackTrace();
        }
        return m;
    }

    @Override
    public PharmacyDrugVo getDrugInfo(Integer id) {
        PharmacyDrugVo pharmacyDruginfo = pharmacyDruginfoMapper.getDrugInfo(id);
        PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
        pharmacyDrugSku.setProductId(id);
        List<PharmacyDrugSku> skus = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku);
        pharmacyDruginfo.setSkuList(skus);
        return pharmacyDruginfo;
    }

    /*
    * 发布非药品
    * */
    @Override
    public int releasedPharmacyNonDrug(PharmacyDrugNonVo pharmacyDrugNonVo) {

        int m = 1;
        try {
            if(StringUtils.isNullOrZero(pharmacyDrugNonVo.getId())){  //表示添加商品
                //添加主表
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyDrugNonVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.insertPharmacyDruginfo(pharmacyDruginfo);


                //添加附加表
                PharmacyHerb pharmacyHerb = new PharmacyHerb();
                BeanUtils.copyProperties(pharmacyDrugNonVo, pharmacyHerb);
                pharmacyHerb.setCreateTime(DateUtils.getNowTimeSecond());
                pharmacyHerb.setPhaId(pharmacyDruginfo.getId());
                pharmacyHerbMapper.insertPharmacyHerb(pharmacyHerb);

                //添加sku表
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyDruginfo.getId());
                List<PharmacyDrugSku> skuList = pharmacyDrugNonVo.getSkuList();
                // 添加sku
                if(skuList!=null){
                    skuList.forEach(sku ->{
                        sku.setProductId(pharmacyDruginfo.getId());
                        pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                    });
                }
            }else{
                //首先修改主表信息
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyDrugNonVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.updatePharmacyDruginfo(pharmacyDruginfo);

                //处理附加表信息
                PharmacyHerb pharmacyHerb = new PharmacyHerb();
                BeanUtils.copyProperties(pharmacyDrugNonVo, pharmacyHerb);
                if(pharmacyDrugNonVo.getDrugId()!=null){ //表示要修改附表的
                    pharmacyHerbMapper.updatePharmacyHerb(pharmacyHerb);
                }else{
                    pharmacyHerb.setCreateTime(DateUtils.getNowTimeSecond());
                    pharmacyHerb.setPhaId(pharmacyDruginfo.getId());
                    pharmacyHerbMapper.insertPharmacyHerb(pharmacyHerb);
                }

                //处理sku
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyDrugNonVo.getId());
                Map<Integer, PharmacyDrugSku> skuMap = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku).stream().collect(Collectors.toMap(it -> it.getId(), it -> it));
                if(skuMap==null){ //直接添加
                    List<PharmacyDrugSku> skuList = pharmacyDrugNonVo.getSkuList();
                    // 添加sku
                    if(skuList!=null){
                        skuList.forEach(sku ->{
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }
                }else{ //修改
                    //针对已有的进行修改
                    List<PharmacyDrugSku> updateList = pharmacyDrugNonVo.getSkuList().stream().filter(it -> it.getId() != null).collect(Collectors.toList());
                    if (updateList!=null) {
                        updateList.forEach(it->{
                            PharmacyDrugSku sku = skuMap.get(it.getId());
                            sku.setUpdateTime(DateUtils.getNowTimeSecond());
                            sku.setCreateTime(DateUtils.getNowTimeSecond());
                            sku.setPrice(it.getPrice());
                            sku.setSpData(it.getSpData());
                            sku.setPic(it.getPic());
                            sku.setStock(it.getStock());
                            pharmacyDrugSkuMapper.updatePharmacyDrugSku(sku);
                        });
                    }

                    //针对没有的进行新增
                    List<PharmacyDrugSku> addList = pharmacyDrugNonVo.getSkuList().stream().filter(it -> it.getId() == null).collect(Collectors.toList());
                    if (addList!=null) {
                        addList.forEach(sku -> {
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }

                    //删除
                    List<Integer> idList = pharmacyDrugNonVo.getSkuList().stream().filter(it -> it.getId() != null).map(it -> it.getId()).collect(Collectors.toList());
                    List<Integer> deleteIds = skuMap.keySet().stream().filter(it -> !idList.contains(it)).collect(Collectors.toList());
                    for(int i=0;i<deleteIds.size();i++){
                        pharmacyDrugSkuMapper.deletePharmacyDrugSkuById(deleteIds.get(i));
                    }

                }
            }
        } catch (BeansException e) {
            m=0;
            e.printStackTrace();
        }
        return m;
    }

    @Override
    public PharmacyDrugVo getDrugInfoNon(Integer id) {

        PharmacyDrugVo pharmacyDruginfo = pharmacyDruginfoMapper.getDrugInfoNon(id);
        PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
        pharmacyDrugSku.setProductId(id);
        List<PharmacyDrugSku> skus = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku);
        pharmacyDruginfo.setSkuList(skus);

        return pharmacyDruginfo;
    }

    /*
    * 发布中草药
    * */
    @Override
    public int releasedPharmacyHerb(PharmacyHerbVo pharmacyHerbVo) {
        int m = 1;
        try {
            if(StringUtils.isNullOrZero(pharmacyHerbVo.getId())){  //表示添加商品
                //添加主表
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyHerbVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.insertPharmacyDruginfo(pharmacyDruginfo);


                //添加附加表
                PharmacyDrug pharmacyDrug = new PharmacyDrug();
                BeanUtils.copyProperties(pharmacyHerbVo, pharmacyDrug);
                pharmacyDrug.setCreateTime(DateUtils.getNowTimeSecond());
                pharmacyDrug.setPhaId(pharmacyDruginfo.getId());
                pharmacyDrugMapper.insertPharmacyDrug(pharmacyDrug);

                //添加sku表
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyDruginfo.getId());
                List<PharmacyDrugSku> skuList = pharmacyHerbVo.getSkuList();
                // 添加sku
                if(skuList!=null){
                    skuList.forEach(sku ->{
                        sku.setProductId(pharmacyDruginfo.getId());
                        pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                    });
                }
            }else{
                //首先修改主表信息
                PharmacyDruginfo pharmacyDruginfo = new PharmacyDruginfo();
                BeanUtils.copyProperties(pharmacyHerbVo, pharmacyDruginfo);
                pharmacyDruginfo.setUpdateTime(DateUtils.getNowTimeSecond());
                pharmacyDruginfoMapper.updatePharmacyDruginfo(pharmacyDruginfo);

                //处理附加表信息
                PharmacyHerb pharmacyHerb = new PharmacyHerb();
                BeanUtils.copyProperties(pharmacyHerbVo, pharmacyHerb);
                if(pharmacyHerbVo.getDrugId()!=null){ //表示要修改附表的
                    pharmacyHerb.setId(pharmacyHerbVo.getDrugId());
                    pharmacyHerbMapper.updatePharmacyHerb(pharmacyHerb);
                }else{
                    pharmacyHerb.setCreateTime(DateUtils.getNowTimeSecond());
                    pharmacyHerb.setPhaId(pharmacyDruginfo.getId());
                    pharmacyHerbMapper.insertPharmacyHerb(pharmacyHerb);
                }

                //处理sku
                PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
                pharmacyDrugSku.setProductId(pharmacyHerbVo.getId());
                Map<Integer, PharmacyDrugSku> skuMap = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku).stream().collect(Collectors.toMap(it -> it.getId(), it -> it));
                if(skuMap==null){ //直接添加
                    List<PharmacyDrugSku> skuList = pharmacyHerbVo.getSkuList();
                    // 添加sku
                    if(skuList!=null){
                        skuList.forEach(sku ->{
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }
                }else{ //修改
                    //针对已有的进行修改
                    List<PharmacyDrugSku> updateList = pharmacyHerbVo.getSkuList().stream().filter(it -> it.getId() != null).collect(Collectors.toList());
                    if (updateList!=null) {
                        updateList.forEach(it->{
                            PharmacyDrugSku sku = skuMap.get(it.getId());
                            sku.setUpdateTime(DateUtils.getNowTimeSecond());
                            sku.setCreateTime(DateUtils.getNowTimeSecond());
                            sku.setPrice(it.getPrice());
                            sku.setSpData(it.getSpData());
                            sku.setPic(it.getPic());
                            sku.setStock(it.getStock());
                            pharmacyDrugSkuMapper.updatePharmacyDrugSku(sku);
                        });
                    }

                    //针对没有的进行新增
                    List<PharmacyDrugSku> addList = pharmacyHerbVo.getSkuList().stream().filter(it -> it.getId() == null).collect(Collectors.toList());
                    if (addList!=null) {
                        addList.forEach(sku -> {
                            sku.setProductId(pharmacyDruginfo.getId());
                            pharmacyDrugSkuMapper.insertPharmacyDrugSku(sku);
                        });
                    }

                    //删除
                    List<Integer> idList = pharmacyHerbVo.getSkuList().stream().filter(it -> it.getId() != null).map(it -> it.getId()).collect(Collectors.toList());
                    List<Integer> deleteIds = skuMap.keySet().stream().filter(it -> !idList.contains(it)).collect(Collectors.toList());
                    for(int i=0;i<deleteIds.size();i++){
                        pharmacyDrugSkuMapper.deletePharmacyDrugSkuById(deleteIds.get(i));
                    }

                }
            }
        } catch (BeansException e) {
            m=0;
            e.printStackTrace();
        }
        return m;
    }

    @Override
    public PharmacyDrugVo getDrugInfoHerb(Integer id) {
        PharmacyDrugVo pharmacyDruginfo = pharmacyDruginfoMapper.getDrugInfoHerb(id);
        PharmacyDrugSku pharmacyDrugSku = new PharmacyDrugSku();
        pharmacyDrugSku.setProductId(id);
        List<PharmacyDrugSku> skus = pharmacyDrugSkuMapper.selectPharmacyDrugSkuList(pharmacyDrugSku);
        pharmacyDruginfo.setSkuList(skus);

        return pharmacyDruginfo;
    }
}
